package com.wbj.vpdirection.viewpager

import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.wbj.vpdirection.mode.AngleData
import com.wbj.vpdirection.R
import com.wbj.vpdirection.util.AltitudeUtil.dipToPx

open class DirectionView : LinearLayout {
    
    val TAG = this::class.java.simpleName
    val mAngleRange = 45
    val mAnglePI = 360
    val mAngleLimit = 22.5
    var mWidth: Int = 150
    var mHeight: Int = 50
    val mDrawableWidth:Int by lazy { dipToPx(context,15f) }
    val mDrawableHeight:Int by lazy { dipToPx(context,7.5f) }
    val mInflater: LayoutInflater by lazy { LayoutInflater.from(context) }
    val dropDownDrawable: Drawable = context.resources.getDrawable(R.drawable.drop_down, context.theme)
    val mPaint: Paint by lazy { Paint(Paint.ANTI_ALIAS_FLAG) }
    val mFadingEdgeLength: Int = 400
    
    lateinit var mRootView: View
    lateinit var viewPager: ViewPager
    lateinit var list:MutableList<AngleData>
    
    var centerView: TextView? = null
    
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr){
        initData()
        initView()
    }
    
    
    private fun initView() {
        mRootView = mInflater.inflate(R.layout.layout_direction_viewpager,this)
        
        viewPager = mRootView.findViewById(R.id.viewPager)
        
        viewPager.adapter = DirectionPageAdapter(context,list)
        viewPager.addOnPageChangeListener(mPageListener)
        viewPager.currentItem = 2
        
    }
    
    private fun initData(){
        list = mutableListOf()
        for (i in 0..7){
            if (i==0){
                list.add(AngleData(345,0,15))
            }else{
                list.add(AngleData((3*i-1)*15,3*i*15,(3*i+1)*15))
            }
        }
        //多出两个元素进行循环
        list.add(0, AngleData(300,315,330))
        list.add(0, AngleData(355,270,285))
        list.add(AngleData(345,0,15))
        list.add(AngleData(30,45,60))
        
    }
    
    open fun setAngle(angle:Float){
        angle.let {
            //取余360，除以22.5，toInt
            var rate = it.rem(mAnglePI).div(mAngleRange).toInt()
            if ((angle.minus(rate.times(mAngleRange))) >= mAngleLimit ){
                rate ++
            }
            //当前数据 2-9有效，0、1、10、11无效，所以从1开始，实际数据要自增
            rate+=2
            viewPager.setCurrentItem(rate,true)
            
        }
    }
    
    
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val wSize = MeasureSpec.getSize(widthMeasureSpec)
        val hSize = MeasureSpec.getSize(heightMeasureSpec)
        if (mWidth <= wSize) {
            mWidth = wSize
        }
        if (mHeight <= hSize) {
            mHeight = hSize
        }
        setMeasuredDimension(mWidth, mHeight)
    }
    
    override fun dispatchDraw(canvas: Canvas) {
        super.dispatchDraw(canvas)
        mPaint.color = Color.BLACK
//        canvas.drawRect((mWidth - mPaint.measureText("东北")) / 2f,
//            0f,
//            (mWidth + mPaint.measureText("东北")) / 2f,
//            mDrawableWidth.toFloat(),
//            mPaint)
        dropDownDrawable.setBounds((mWidth - mDrawableWidth) / 2,
            0,
            (mWidth + mDrawableWidth) / 2,
            mDrawableHeight)
        dropDownDrawable.draw(canvas)
        mPaint.color = Color.parseColor("#419C3A")
    
    
        val shader = LinearGradient(0f, 0f, 0f, 1f, -0x1000000, 0, Shader.TileMode.CLAMP)
        val paint = Paint()
        paint.shader = shader
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT)
       
    }
    
    private val mPageListener = object : ViewPager.OnPageChangeListener {
        
        var currentPosition = 0
        
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {

        }
        
        override fun onPageSelected(position: Int) {
            currentPosition = position
            if (list.size > 1){
                if (currentPosition == 1){
                    currentPosition = list.size - 3
                    viewPager.setCurrentItem(currentPosition,false)
                }else if (currentPosition == list.size -2){
                    currentPosition = 2
                    viewPager.setCurrentItem(2,false)
                }
            }
            Log.e(TAG,"onPageSelected:pos:${position}")
        }
        
        override fun onPageScrollStateChanged(state: Int) {
            Log.e(TAG,"onPageScrollStateChanged:state:${state}")
            
        }
        
    }
    
    fun setPageMargin(margin: Float) {
        viewPager.pageMargin = dipToPx(context,margin)
    }
    
    fun setOffsetScreenPageLimit(size:Int){
        size.let {
            viewPager.offscreenPageLimit = size
        }
    }
}

